﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using System.Configuration;

namespace eCommerce.web.Controls.Common
{
    public partial class Footer : System.Web.UI.UserControl
    {
        protected string path = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                path = Utils.GetPathImg();

                ListCategory();
            }
        }

        private void ListCategory()
        {
            var lstCategory = ListCategoryByParentId((int) CategoryType.Root);
            if (lstCategory.Count > 0)
            {
                rptCategory.DataSource = lstCategory;
                rptCategory.DataBind();
            }
        }        

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var categoryId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
                var rptSubCategory = (Repeater) e.Item.FindControl("rptSubCategory");

                if (categoryId > 0)
                {
                    rptSubCategory.DataSource = ListCategoryByParentId(categoryId);
                    rptSubCategory.DataBind();

                    
                }
            }
        }

        protected void rptSubCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var categoryId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfSubCategoryId")).Value);
                var ltrHref = (Literal)e.Item.FindControl("ltrHref");

                var lstArticle = ArticleBo.GetByCatId(categoryId);
                if (lstArticle != null)
                {
                    ltrHref.Text = "<a href=\"/" +lstArticle.CategoryUrl+ "/" + lstArticle.Url + "-a-"+lstArticle.ArticleId+".htm\">";
                }
            }
        }
       
        private List<CategoryEntity> ListCategoryByParentId(int categoryId)
        {


            return CategoryBo.GetListByParentId((int)CategoryType.Guide, categoryId, (int)CategoryType.Active);


        }
    }
}